from flask import Flask, render_template, session,g
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import text, desc, func, or_
from flask_migrate import Migrate
import config
from exts import db,mail
from models import UserModel
from blueprints.auth import bp as auth_bp
from blueprints.qa import bp as qa_bp

app = Flask(__name__)

app.config.from_object(config)

db.init_app(app)
mail.init_app(app)

app.register_blueprint(qa_bp)
app.register_blueprint(auth_bp)

migrate = Migrate(app, db)

#定义全局变量
@app.before_request
def before_request():
    user_id = session.get('user_id')
    if user_id:
        user = UserModel.query.get(user_id)
        setattr(g, 'user', user)
    else:
        g.user = None
#
@app.context_processor
def inject_user():
    return {'user': g.user}

if __name__ == '__main__':
    app.run()
